<?php

/*----------------------------------------------+
|  MaxForum					|
|  ===========================================	|
|  By Majd Almontaser				|
|  Released under the License GNU v3.0		|
|  http://www.Max4Dev.com			|
|  ===========================================	|
|  Ttmtt Team - http://www.liioiil.com		|
+-----------------------------------------------*/

if (!defined('MAX_ON')){
	echo "<h1>ACCESS DENIED</h1>You cannot access this file directly.";
	exit();
}

class Image
/**
Image handling class.
Checks uploaded image, creates thumbnails, reflections, etc
**/
{
	private $name, $size, $tmp, $ext, $width, $height, $settings, $thumbInfo, $admin;
	
	public function __construct($imageArray, $settings, $admin = false)
	{
		if (!is_uploaded_file($imageArray['tmp_name'])) return false;
		
		$this->name	= $imageArray['name'];
		$this->size	= $imageArray['size'];
		$this->tmp	= $imageArray['tmp_name'];
		
		$settings['image_extensions'] = explode(',', $settings['image_extensions']);
		$this->settings = $settings;
		
		$this->admin = $admin;
	}
	
	public function check()
	{
		if ($this->name == '' || $this->size == 0 || $this->tmp == '') return false;
		
		# Get the file's extension
		$ext = strtolower(substr($this->name, strrpos($this->name, '.') + 1));
		
		if (!$this->admin)
		{			
			# Check its an allowed file type
			if (!in_array($ext, $this->settings['image_extensions'])) return false;
			
			# Make sure it's not too large
			if ($this->size > ($this->settings['image_max_size'] * 1024)) return false;
		}
		
		$this->ext = $ext;
		
		return true;
	}
	
	private function tInit()
	/**
	Work out thumbnail dimensions
	**/
	{
		# Work out the two lengths
		list($width, $height) = getimagesize($this->tmp);
		
		$this->width	= $width;
		$this->height	= $height;
		
		#--------------------------------------------------------
		# First check whether the image is too wide
		#--------------------------------------------------------
		
			if ($width > $this->settings['t_max_width'])
			{
				list($newWidth, $newHeight) = $this->newDim('w', $width, $height);
				
				# But the height could still be too large?
				if ($newHeight > $this->settings['t_max_height'])
				{
					list($newWidth, $newHeight) = $this->newDim('h', $width, $height);
				}
			}
		
		#----------------------------------------
		# It might be too tall then?
		#----------------------------------------
		
			elseif ($height > $this->settings['t_max_height'])
			{
				list($newWidth, $newHeight) = $this->newDim('h', $width, $height);
			}
		
		#----------------------------------------
		# No resizing needed
		#----------------------------------------
		
			else
			{
				$newWidth	= $width;
				$newHeight	= $height;
			}
			
		return array($newWidth, $newHeight);
	}
	
	private function newDim($type, $width, $height)
	/**
	Works out resized dimensions
	**/
	{
		if ($type == 'w')
		
			$scale = $width / $this->settings['t_max_width'];
		
		elseif ($type == 'h')
		
			$scale = $height / $this->settings['t_max_height'];
			
		return array($width / $scale, $height / $scale);
	}
	
	public function upload($dir, $user_id)
	/**
	Upload image, do thumbnail and add database record
	**/
	{
		#------------------------------------------------
		# Create a unique name for our image
		#------------------------------------------------
		
			$user_id = (int) $user_id;
			if ($user_id == 0) return false;
			
			$time = time();
			$imgName = $user_id . '_' . $time . '_' . $this->name;
		
		#-----------------------------------------
		# Upload the full-sized image
		#-----------------------------------------
		
			# We need thumbnail dimensions before the file gets sent away
			list($tWidth, $tHeight) = $this->tInit();
			if (!move_uploaded_file($this->tmp, $dir . $imgName)) return false;
		
		#-----------------------------------------
		# Lets create the thumbnail
		#-----------------------------------------
		
			$imgNameT = 't_' . $imgName;
		
			# If resized dimensions are the same, we don't have to go through the process of creating a thumbnail
			if ($tWidth == $this->width && $tHeight == $this->height)
			{
				if (!copy($dir . $imgName, $dir . $imgNameT)) return false;
			}
			else
			{
				switch($this->ext)
				{
					case 'jpg':
					case 'jpeg':
						$src = imagecreatefromjpeg($dir . $imgName);
						break;
					
					case 'gif':
						$src = imagecreatefromgif($dir . $imgName);
						break;		
					
					case 'png':
						$src = imagecreatefrompng($dir . $imgName);
						break;
				}
				
				# Create a blank canvas with the resized dimensions.
				$tmp = imagecreatetruecolor($tWidth, $tHeight);
				
				# Now take the whole of the original image, and dump it onto the image created above.
				imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tWidth, $tHeight, $this->width, $this->height);
				
				# Decide which method to use to save the image
				switch ($this->ext)
				{
					case 'jpg':
					case 'jpeg':
					
						imagejpeg($tmp, $dir . $imgNameT);
						break;
					
					case 'gif':
					
						imagegif($tmp, $dir . $imgNameT);
						break;
					
					case 'png':
					
						imagepng($tmp, $dir . $imgNameT);
						break;
				}
				
				# Get rid of the temporary images
				imagedestroy($src);
				imagedestroy($tmp);
			}
			
			# Create reflection if they're turned on
			if ($this->settings['image_reflections'] == 1)
			
				include 'reflect.php';
		
		if ($this->admin) unlink($dir . $imgName);
			
		return $imgName;
	}
}
?>
